package com.woniuxy.controller;

import com.woniuxy.dao.ProductRepo;
import com.woniuxy.entity.Img;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.ProductOrder;
import com.woniuxy.service.LiuService;
import com.woniuxy.service.impl.LiuServiceImpl;
import com.woniuxy.util.Page;
import com.woniuxy.util.QiniuCloudUtil;
import com.woniuxy.util.Result;
import com.woniuxy.vo.ProductVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/*
@author Jan
@create 2020-06-02 12:35

*/
@RestController
public class ControllerLiu {
    @Resource
    private LiuService liuService;
    @Resource
    private ProductRepo productRepo;

    //分页查询商品
    @RequestMapping("/selectAllProductFenYe")
    public Result selectAllProductFenYe(int pageIndex, int number, int categoryId, int status,String condition) {
        System.out.println("condition:"+condition);
        //获取总数量
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("categoryId", categoryId);
        map1.put("status", status);
        map1.put("condition",condition);
        int count = liuService.selectAllProduct(map1).size();
        System.out.println("总页数"+count);
        //总页数
        int pageCount = count % number == 0 ? count / number : (count / number + 1);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageIndex", pageIndex);
        map.put("number", number);
        map.put("categoryId", categoryId);
        map.put("status", status);
        map.put("condition",condition);
        List<Product> list = liuService.selectAllProduct(map);
        Page page = new Page();
        page.setCount(count);
        page.setPageCount(pageCount);
        page.setNumber(number);
        page.setPageIndex(pageIndex);
        return new Result("200", "success", page, list);
    }

    //查询该proId所有图片
    @RequestMapping("/selectProductAllImgs")
    public Result selectProductAllImgs(int proId) {
        List<Img> list = liuService.selectProductAllImg(proId);
        return new Result("200", "success", null, list);
    }

    //上下架商品
    @RequestMapping("/updateProductStatus")
    public Result updateProductStatus(int proId, int status) {
        Product product = new Product();
        product.setProId(proId);
        product.setStatus(status);
        int i = liuService.updateProductStatus(product);
        if (i == 1) {
            return new Result("200", "success", null, null);
        } else {
            return new Result("400", "defeat", null, null);
        }
    }

    @RequestMapping("/uploadImgs")
    public String uploadImgs(MultipartFile file) {
        MultipartFile avatar = file;
        System.out.println("参数：" + avatar);
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "_" + avatar.getOriginalFilename();
        try {
            System.out.println("文件名：" + fileName);
            QiniuCloudUtil.upload(fileName, avatar.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(QiniuCloudUtil.fullPath(fileName)+"============");
        //返回最终存入数据库的地址
        return QiniuCloudUtil.fullPath(fileName);
    }

    //添加商品返回主键
    @RequestMapping("/addProductLiu")
    public Result addProductLiu(Product product,String url) {
        System.out.println("url:"+url);
        liuService.insertProductReturnProId(product);
        Integer proId = product.getProId();
        //存入elasticSearch
        ProductVo vo = new ProductVo();
        vo.setProId(product.getProId());
        vo.setCategoryId(product.getCategoryId());
        vo.setDescribe(product.getDescribe());
        vo.setProName(product.getProName());
        vo.setPrice(product.getPrice());
        vo.setStatus(product.getStatus());
        vo.setUrl(url);
        vo.setStock(product.getStock());
        productRepo.save(vo);
        return new Result("200", "success", proId, null);
    }

    //新增图片
    @RequestMapping("/addProductImg")
    public Result addProductImg(int proId, String url) {
        int i = liuService.insertProductImg(proId, url);
        return new Result("200", "success", i, null);
    }

    //删除图片
    @RequestMapping("/deleteProductImg")
    public Result deleteProductImg(int imgId){
        int i = liuService.deleteProductImg(imgId);
        return new Result("200","success",null,null);
    }

    //跟新product商品
    @RequestMapping("/updateProductLiu")
    public Result updateProductLiu(Product product){
        liuService.updateProduct(product);
        //存入elasticSerarch
        ProductVo vo = new ProductVo();
        vo.setProId(product.getProId());
        vo.setCategoryId(product.getCategoryId());
        vo.setDescribe(product.getDescribe());
        vo.setProName(product.getProName());
        vo.setPrice(product.getPrice());
        vo.setStatus(product.getStatus());
        vo.setStock(product.getStock());
        //找到出图片,放一张,以防elastic上的图片在此次更新被删除了
        List<Img> list = liuService.selectProductAllImg(product.getProId());
        if(list.size()>0){
            vo.setUrl(list.get(0).getUrl());
        }
        //存
        productRepo.save(vo);
        return new Result("200","success",null,null);
    }

    //分页查询status,0为所有,1未支付,2发货3,等待收货,4交易完成
    @RequestMapping("/selectAllProductOrderFenYe")
    public Result selectAllProductOrderFenYe(int pageIndex,int number,int status){
        //总页数
        int count = liuService.selectOrderAllCount(status);
        //多少页
        int pageCount=count%number==0?count/number:(count/number+1);
        //获取集合
        List<ProductOrder> list = liuService.selectAllProductOrder(pageIndex, number, status);
        Page page = new Page();
        page.setPageIndex(pageIndex);
        page.setNumber(number);
        page.setPageCount(pageCount);
        page.setCount(count);
        return new Result("200","success",page,list);
    }

    //确定收货,改变订单状态为3
    @RequestMapping("/updateProductOrderStatusThree")
    public Result updateProductOrderStatusThree(int proOrderId){
        int i = liuService.updateProducOrderStatus(proOrderId);
        return new Result("200","success",i,null);
    }
}
